Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

411
Vistas
How to get the result of $wpdb ->query to know if it was successful

I am currently working on a wordpress site,

I am using a form that the user can modify so that it updates the database :

Here's the HTML/PHP code :

echo '<form class="form-verifdoc" target="noredirect" method = "post">';
echo '<label class="label-verifdoc" style="float:left;margin-top:10px;margin-right:10px;"for="'.$name_input_verifdoc.'">'.$label_element.'</label><textarea style="float:right;margin-bottom:10px;vertical-align:middle!important;border-left: 5px solid black;resize:vertical;" rows="1" type="text" name="variables['.$name_input_verifdoc.']">'.$contenu_input_verifdoc.'</textarea>';
echo'<input name="idchamps['.$id_input_verifdoc.']" type="hidden" value="'.$id_input_verifdoc.'">';
echo '<div class="div_edit_verifdoc">';
echo '<input type="submit" id="'.$id_input_verifdoc.'" class="bouton-edit-champ-doc" onclick="return confirm(\'Voulez-vous vraiment modifier les informations de ce champ ?\') && AlertQuery()" name="bouton-envoi-editform" value="Editer le champ"/>';
echo '</div>';
echo '<iframe name="noredirect" style="display:none;"></iframe>';
echo '</form>';

I use a confirm so that the user has to confirm that he wants to update the data.

And here's the script that use the wpdb query to update the MySQL data :

 <script>
<?php

 if (isset($_POST['bouton-envoi-editform']) && !empty($_POST['bouton-envoi-editform'])) {

   foreach($_POST['variables'] as $variableName => $variable){
     $ValeurChampModif = $variable;
   }

   foreach($_POST['idchamps'] as $champid => $idchamp){
     $Champ_id = $idchamp;
   }

$modif_doc_bdd = $wpdb->query($wpdb->prepare("UPDATE `mod114_frmt_form_entry_meta_copie` SET  `meta_value` = '$ValeurChampModif' WHERE `mod114_frmt_form_entry_meta_copie`.`meta_id` =    $Champ_id;"));

} else {

}

?> 

function AlertQuery($modif_doc_bdd){

  <?php
  if( $modif_doc_bdd ){
    ?>
    alert('Les modifications ont bien été enregistrées.');
  <?php
  }else{
    ?>
    alert('⚠ ERREUR, les modifications n\'ont pas pu être enregistrées. Veuillez ré-essayer.');
  <?php
  }
  ?>
}
    </script>

I am trying to get the function AlertQuery to make a popup alert if the wpdb query was successful or not when updating the data.

But I can't seem to grab the response of the $wpdb query...

I have tried to put the function to have everything inside but that didn't work as it wouldn't prompt the popup.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you use $wpdb->update() it returns the number of rows updated on success or false on error , if you then check if $wpdb->update() is false or not you can call your AlertQuery() method accordingly.

I hope I kind of explained properly, see docs here: https://developer.wordpress.org/reference/classes/wpdb/update/#return

about 4 years ago · Juan Pablo Isaza Denunciar

0

$modif_doc_bdd = $wpdb->query($wpdb->prepare("UPDATE `mod114_frmt_form_entry_meta_copie` SET  `meta_value` = '$ValeurChampModif' WHERE `mod114_frmt_form_entry_meta_copie`.`meta_id` =    $Champ_id;"));
 
if ( false === $modif_doc_bdd ) {
    // There was an error.
} else {
    // No error. You can check updated to see how many rows were changed.
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda